<?php
/**
* Update the user preferences
*
* @author       Fabrice Douteaud <clearbudget@douteaud.com>
* @package      framework
* @access       public
*/

/***********************************************************************

  Copyright (C) 2008  Fabrice douteaud (clearbudget@douteaud.com)

    This file is part of ClearBudget.

    ClearBudget is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClearBudget is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClearBudget.  If not, see <http://www.gnu.org/licenses/>.


************************************************************************/

// some flags
$badUsername = false;
$badPassword = false;
$settingsSuccess = false;

// if the user did not submit, we simply go to the view
if(!isset($_REQUEST['submit'])) {
  // if a language is set, we save it in the DB
  if(isset($_REQUEST['setupLanguage'])) {
    class_settings::setLanguage($_REQUEST['setupLanguage']);
    }
  return;
  }

// if we have the request to secure the access to the site, we must have a valid username and password
if(isset($_REQUEST['secureAccess']) && $_REQUEST['secureAccess'] == '1') {
  $username = $_REQUEST['username'];
  $password = $_REQUEST['password'];
  // if username and password are valid
  if($username == null || $username == '' || strlen($username)<6) $badUsername = true;
  if($password == null || $password == '' || strlen($password)<6) $badPassword = true;
  if($badUsername || $badPassword) {
    $_REQUEST['secureAccess'] = null;
    }
  }

$language = $_REQUEST['language'];
$currency = $_REQUEST['currency'];
// set the setup cookie if mandatory information are present
if($language!='NULL' && $currency!='NULL' && $language!='' && $currency!='') {
  $settingsSuccess = true;
  $setupDone = true;
  // update the user settings
  class_settings::updateUserSettings($setupDone);
  }
else {
  $setupDone = false;
  }

// switch to the new language if necessary
class_propertyKey::switchToLang(class_settings::$language);
?>